package com.deng.wxmessagecallback.utils.message;

import com.deng.wxmessagecallback.config.MsgType;
import com.deng.wxmessagecallback.config.apollo.ApolloConfig;
import com.deng.wxmessagecallback.utils.CommonUtils;
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutNewsMessage;
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;

/**
 * @author wbdengtt
 * @date 2021/1/11 17:16
 * 欢迎语模版
 */
public class WelcomeTemplate {
    /**
     * 返回图文消息的欢迎模版
     *
     * @return 返回图文消息的欢迎模版 返回的是加密后的
     */
    public static String getNewsMessage(String fromUserName, String toUserName) {
        String title = ApolloConfig.getBaseValue("WxCpWelcomeMessage-title");
        String description =  ApolloConfig.getBaseValue("WxCpWelcomeMessage-description");
        String picUrl =  ApolloConfig.getBaseValue("WxCpWelcomeMessage-picUrl");
        String url = ApolloConfig.getBaseValue("WxCpWelcomeMessage-url");
        WxCpXmlOutNewsMessage.Item item = new WxCpXmlOutNewsMessage.Item();
        item.setDescription(description);
        item.setPicUrl(picUrl);
        item.setUrl(url);
        item.setTitle(title);
        WxCpXmlOutMessage wxCpXmlOutMessage = WxCpXmlOutMessage.NEWS().addArticle(item).build();
        wxCpXmlOutMessage.setMsgType(MsgType.NEWS);
        wxCpXmlOutMessage.setCreateTime(System.currentTimeMillis());
        wxCpXmlOutMessage.setFromUserName(fromUserName);
        wxCpXmlOutMessage.setToUserName(toUserName);
        String toXml = XStreamTransformer.toXml((Class) wxCpXmlOutMessage.getClass(), wxCpXmlOutMessage);
        String token = CommonUtils.sToken;
        String encodingAesKey = CommonUtils.sEncodingAESKey;
        String appidOrCorpid = CommonUtils.sCorpID;
        WxCryptUtil wxCryptUtil = new WxCryptUtil(token, encodingAesKey, appidOrCorpid);
        return wxCryptUtil.encrypt(toXml);
    }
}
